package com.krystal;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author Krystal1
 * @date 2021/4/24 -17:08
 * 使用Jsoup实现页面解析
 */
public class JsoupTest {

    @Test
    public void testGetDocument() throws Exception {
        //Document doc = Jsoup.connect("http://www.itcast.cn/").get();
        // Document doc = Jsoup.parse(new URL("http://www.itcast.cn/"), 1000);
        //Document doc = Jsoup.parse(new File("jsoup.html"), "UTF-8");
        String htmlStr = FileUtils.readFileToString(new File("jsoup.html"), "UTF-8");
        Document doc = Jsoup.parse(htmlStr);
        System.out.println(doc);
        Element titleElement = doc.getElementsByTag("title").first();
        String title = titleElement.text();
        System.out.println(title);
    }

    @Test
    public void testGetElement() throws Exception {

        Document doc = Jsoup.parse(new File("jsoup.html"), "UTF-8");
        System.out.println(doc);

        //根据id获取元素getElementById
        Element element = doc.getElementById("city_bj");
        String text = element.text();
        System.out.println(text);
        //根据标签获取元素getElementsByTag
        Elements elements = doc.getElementsByTag("title");
        Element titleElement = elements.first();
        String title = titleElement.text();
        System.out.println(title);
        //根据class获取元素getElementsByClass
        Element el = doc.getElementsByClass("s_name").last();
        String text1 = el.text();
        System.out.println(text1);
        //根据属性来获取元素getElementsByAttribute
        Element abc = doc.getElementsByAttribute("abc").first();
        String text2 = abc.text();
        System.out.println(text2);

    }


    @Test
    public void testElementOperator() throws Exception {
        Document doc = Jsoup.parse(new File("jsoup.html"), "UTF-8");
        Element ele = doc.getElementsByAttributeValue("class", "city_con").first();
        String id = ele.id();
        System.out.println(id);
        //获取元素中的classname
        String className = ele.className();
        System.out.println(className);
        //获取元素中的属性值
        String id1 = ele.attr("id");
        System.out.println(id1);
        //从元素获取所有的属性
        String attrs = ele.attributes().toString();
        System.out.println(attrs);
        //获取文本内容
        String text = ele.text();
        System.out.println(text);
    }

    @Test
    public void testSelect() throws Exception {

        Document doc = Jsoup.parse(new File("jsoup.html"), "UTF-8");
        //根据标签名获取元素
        Elements spans = doc.select("span");
        for (Element span : spans) {
            System.out.println(span.text());
        }

        //根据id获取元素
        String text = doc.select("#city_bj").text();
        System.out.println(text);
        //根据class获取元素
        String text1 = doc.select(".class_a").text();
        System.out.println(text1);
        //根据属性获取元素
        String text2 = doc.select("[abc]").text();
        System.out.println(text2);

        //根据属性值来获取元素
        String text3 = doc.select("[class=s_name]").text();
        System.out.println(text3);
    }

    @Test
    public void testSelect2() throws Exception {
        Document doc = Jsoup.parse(new File("jsoup.html"), "UTF-8");
        //根据元素+id组合 选取元素
        String text = doc.select("li#test").text();
        System.out.println(text);
        //根据标签名+class
        String text1 = doc.select("li.class_a").text();
        System.out.println(text1);
        //根据标签名+元素
        String text2 = doc.select("span[abc]").text();
        System.out.println(text2);
        //任意组合
        String text3 = doc.select("span[abc].s_name").text();
        System.out.println(text3);
        //查找某个元素下的直接子元素
        String text4 = doc.select(".city_con >ul >li").text();
        System.out.println(text4);
        //查找某个元素下的所有子元素
        String text5 = doc.select(".city_con li").text();
        System.out.println(text5);

    }
}
